(function() {
	window.common = {
		//ajax
		init: function() {
			common.urls = 'http://192.168.0.116:8081/api/'
			common.data = JSON.parse(localStorage.getItem("data"))
			if(common.data == null) {
				return
			}
			common.uid = common.data.uid
			common.token = common.data.token
			console.log(common.data.token)
			var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE') != -1;
			if(isIPHONE) {
				var obj = $('input')
				$("body").on('tap', function() {
					obj.blur();
				});
			}
		},
		getToken: function() {
			common.ajax('/getToken', {
				'uid': common.uid
			}, function(data) {
				if(data.code == 200) {
					common.token = data.data;
				}
			});
		},
                
		ajax: function(url, deta, callback, type) {
			//112.74.17.151:8090  
			//192.168.0.116:8080
			//192.168.0.116:8081/
			mui.ajax(common.urls + url, {
				
				data: deta,
				dataType: 'json', //服务器返回json格式数据
				type: type || 'post', //HTTP请求类型
				timeout: 10000, //超时时间设置为10秒；
				beforeSend: function() {
					var txt = '<div class="sk-body"> <div class="sk-fading-circle"><div class="sk-circle2 sk-circle"></div> <div class="sk-circle3 sk-circle"></div><div class="sk-circle4 sk-circle"></div><div class="sk-circle5 sk-circle"></div><div class="sk-circle6 sk-circle"></div><div class="sk-circle7 sk-circle"></div><div class="sk-circle8 sk-circle"></div><div class="sk-circle9 sk-circle"></div><div class="sk-circle10 sk-circle"></div> <div class="sk-circle11 sk-circle"></div><div class="sk-circle12 sk-circle"></div></div></div>'
					$("body").append(txt)
					$(".sk-body").eq(0).css("display", "block")
				},
				complete: function() {
					$(".sk-body").remove()
				},
				success: function(data) {
					if(data.code == 300) {
						mui.toast("信息为空!")
					}
					//处理token过期
					if(data.code == 4001) {
						mui.toast("网络异常请重试！");
						common.getToken();
					}
					if(data.code == 4002 || data.code == 4003) {
						mui.openWindow({
							url: '/html/loginregistration/login.html',
							id: 'login.html',
							waiting: {
								autoShow: false
							},
							show: {
								aniShow: "none"
							}
						})
					}
					callback(data)
				},
				error: function(type) {
					mui.toast("网路异常")
				}
			});
		},

//		task: function() {
//			plus.uploader.createUpload("http://192.168.0.116:8081/api/student/reading/publish", {
//					method: "POST",
//					//						blocksize: 204800,
//					//						priority: 100
//				},
//				function(t, status) {
//					// 上传完成
//					if(status == 200) {
//						/*mui.toast("发布成功")*/
//						$(".success").css("display", "block")
//					} else {
//						success = false
//						alert("Upload failed: " + status);
//					}
//					wt.close()
//				}
//			);
//		},

		//获取光标(标签,插入的标签)
		focu: function(selector, inserttag) {
			$(selector).on("focus", function() {
				$(this).addClass(inserttag)
			})
			$(selector).on("blur", function() {
				$(selector).removeClass(inserttag)
			})
		},
		//倒计时
		//countdown(标签,到数时间,背景颜色,到数完的背景颜色)
		countdown: function(selector, time, countdowncolor, color, texts) {
			var temp = time;
			jishi()

			function jishi() {
				if(temp >= 0) {
					$(selector).text(temp + "秒")
					$(selector).css({
						"background": countdowncolor,
						"pointer-events": "none"
					})
					setTimeout(jishi, 1000)
				} else {
					$(selector).css({
						"background": color,
						"pointer-events": "inherit"
					})
					$(selector).text(texts)
				}
				temp--
			}
		},
		open: function(urls, id, temp) {
			mui.openWindow({
				url: urls,
				id: id,
				extras: {
					parameter: temp
				},
				waiting: {
					autoShow: false, //自动显示等待框，默认为true
					//					title: '...',
					options: {

					}
				}
			});
		},
		clsopen: function() {
			$(".clsopen").on("tap", function() {
				if($(this).attr("href") == "" || $(this).attr("href") == null) {
					return
				}
				common.open($(this).attr("href"), "", "")
			})
		}
	}
	common.init()
	mui.plusReady(function() {
		plus.screen.lockOrientation("portrait-primary");
		plus.push.addEventListener("click", function(msg) {
			// 判断是从本地创建还是离线推送的消息
			var payload = JSON.parse(msg.payload);
			var url = '';
			switch(payload.messageCode) {
				case "1001":
					url = "/html/reading/TeacherProgram.html";
					break;
				case "1002":
					url = "/html/langdu/emptyreading.html";
					break;
				default:
					url = '/html/home/home.html';
					break;
			}
			// 提示点击的内容
			mui.openWindow({
				url: url,
				id: 'tui'
			});
		}, false);
		// 监听在线消息事件
		plus.push.addEventListener("receive", function(msg) {

		}, false);
	});
})()